package com.lancoo.ccas52.config;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.lancoo.ccas52.pojo.response.basicPlatform.SubSystemsMainServer;
import com.lancoo.ccas52.service.YunBasicService;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

/**
 * @ClassName RabbitMQConfig
 * @Description 消息队列配置文件
 * @Author quin
 * @Date 2023/3/17 14:04
 */
//@Configuration
//@EnableRabbit
public class RabbitMQConfig {

    @Autowired
    private YunBasicService yunBasicService;

    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        //发现消息中有content_type有text就会默认将其转换成string类型的
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        // factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        SubSystemsMainServer e80 = yunBasicService.getSubSystemsMainServer("E80",true);
        String E80addr = e80.getWsSvrAddr();
        if (!StrUtil.isEmpty(E80addr)) {
            //分割 http://192.168.129.172:10108/ 只要ip地址192.168.129.172
            E80addr = E80addr.split("/")[2].split(":")[0];
        }

        String rabbitMQAddr = E80addr;

        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitMQAddr, 5672);

        connectionFactory.setUsername("messageAdmin");

        connectionFactory.setPassword("Lg123456");

        connectionFactory.setVirtualHost("/");

        connectionFactory.setConnectionTimeout(300000);

        connectionFactory.setPublisherConfirms(true); // 必须要设置这里是回调用的

        return connectionFactory;

    }

    @Bean
    @Scope("prototype")
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());
    }

    @Bean
    public AmqpAdmin amqpAdmin() throws JsonProcessingException {
        return new RabbitAdmin(connectionFactory());
    }

    //配置消费者监听的容器
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() throws JsonProcessingException {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrentConsumers(3);
        factory.setMaxConcurrentConsumers(10);
        //factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);//设置确认模式手工确认
        return factory;
    }


}
